package com.stu.dao.impl;

import com.stu.dao.LoginDao;
import com.stu.model.User;
import com.stu.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDaoImpl implements LoginDao {
    //根据用户账号和用户密码查找用户信息
    @Override
    public User getLogin(User user) throws SQLException {
        //sql语句
        String sql = "select * from user where userAccount='" + user.getUserAccount()
                + "' and userPassword='" + user.getUserPassword() + "'";
       /* System.out.println(sql);*/
        //创建连接
        Connection conn = JdbcUtils.getConnection();
        //执行语句
        PreparedStatement pre =  conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
       //查询结果
        ResultSet res = pre.executeQuery();
        //创建用户对象
        User loginUser=new User();
        //将查询结果封装在用户对象中
        while (res.next()) {
            loginUser.setUserAccount(res.getString("userAccount"));
            loginUser.setUserName(res.getString("userName"));
            loginUser.setUserSex(res.getString("userSex"));
            loginUser.setUserBirthday(res.getString("userBirthday"));
            loginUser.setUserIdCard(res.getString("userIdCard"));
            loginUser.setUserPassword(res.getString("userPassword"));
            loginUser.setUserIdentify(Integer.parseInt(res.getString("userIdentify")));
            loginUser.setUserOtherName(res.getString("userOtherName"));
        /*    System.out.println("dao"+loginUser.getUserName());*/
        }
return loginUser;
    }
}
